package myapps.ttclient.connection;

import java.util.ArrayList;
import java.util.Collections;

import android.content.SharedPreferences;

public abstract class ConnectionList
{
	protected ArrayList<Connection> connections;
	protected SharedPreferences preferences;

	public ConnectionList(SharedPreferences preferences) {
		this.preferences = preferences;
		this.connections = new ArrayList<Connection>();

		this.load();
	}

	public abstract void load();

	public abstract void save();

	//public abstract Connection add();

	public void sort() {
		Collections.sort(this.connections);
	}
	
	public void RemoveAll() {
		this.connections.clear();
		this.usedPosition = -1;
	}

	public Connection remove(int position) {
		if(position == this.usedPosition) {
			this.usedPosition = -1;
			
		} else if(position > this.usedPosition) {
			
			
		} else if(position < this.usedPosition) {
			this.usedPosition--;
			
		}

		return this.connections.remove(position);
	}

	public Connection get(int position) {
		return this.connections.get(position);
	}

	public int getCount() {
		return this.connections.size();
	}

	public Connection use(int position) {
		return this.get(position);
	}

	protected int usedPosition = -1;

	public int getUsedPosition() {
		return usedPosition;
	}

	public void setUsedPosition(int usedPosition) {
		this.usedPosition = usedPosition;
	}

	public Connection getUsed() {
		return (this.usedPosition != -1) ? this.connections.get(this.usedPosition) : null;
	}

	public boolean add(Connection c) {
		try {
			this.connections.add(c);

			return true;

		} catch (Exception ex) {
			return false;
		}
	}
}
